#include <stdio.h>

#include "../sjjg/list.h"

typedef size_t tType;

tType showData(void* p) {
    return *(tType*)p;
}
void showList(ms_list_t* list) {
    size_t c = 0;
    ms_list_item b = ms_list_begin(list);
    while (b && b != ms_list_end(list)) {
        printf("%lu ", showData(b->p));
        b = ms_list_item_next(b);
        ++c;
    }
    printf("\nsize:%lu = %lu\n", c, ms_list_size(list));
}
int getSel() {
    printf("1.push back\n");
    printf("2.insert\n");
    printf("3.push front\n");
    printf("4.front & back\n");
    printf("5.show map\n");
    printf("6.delete\n");
    printf("7.clear\n");
    printf("8.pop front\n");
    printf("9.pop back\n");
    printf("10.sort\n");
    printf("other exit\n");
    printf("select please:");
    int i;
    scanf("%d", &i);
    return i;
}
int getV() {
    int a = rand() % 1000;
    printf("push a value:%d\n", a);
    return a;
}
ms_list_item getWhere(ms_list_t* list, const char* str) {
    unsigned long i = 0;
    printf(str);
    scanf("%lu", &i);
    ms_list_item b = ms_list_begin(list);
    while (i > 0 && b && b != ms_list_end(list)) {
        b = ms_list_item_next(b);
        --i;
    }
    return b;
}
bool list_less(void* left, void* right) {
    return showData(left) < showData(right);
}
int main(int args, char* argv[]) {
    ms_list_t* l = ms_list_create(NULL, sizeof(tType), NULL);
    if (l) {
        do
        {
            ms_list_item w = 0;
            int a = 0;
            int sel = getSel();
            switch (sel) {
            case 1:
                a = getV();
                ms_list_push_back(l, &a);
                break;
            case 2:
                a = getV();
                w = getWhere(l, "enter a pos:");
                ms_list_insert(l, w, &a);
                break;
            case 3:
                a = getV();
                ms_list_push_front(l, &a);
                break;
            case 4:
                printf("f&b:%lu %lu\n", showData(ms_list_front(l)), showData(ms_list_back(l)));
                break;
            case 5:
                showList(l);
                break;
            case 6:
                w = getWhere(l, "enter a pos:");
                ms_list_remove(l, w);
                break;
            case 7:
                ms_list_clear(l);
                break;
            case 8:
                ms_list_pop_front(l);
                break;
            case 9:
                ms_list_pop_back(l);
                break;
            case 10:
                ms_list_sort(l,list_less);
                break;
            default:
                goto exit;
            }
        } while (1);
exit:
        ms_list_destory(l);
    }
    return 0;
}
